/*
 * @Author: smallalso<hu141418@gmail.com>
 * @Date: 2021-02-06 11:34:35
 * @LastEditors: smallalso<hu141418@gmail.com>
 * @LastEditTime: 2021-02-20 10:22:31
 * @FilePath: /winning-web-account-management/src/lib/request_dev/debug.js
 */

export let isDebugger = false

function createRandomId () {
  return (Math.random() * 10000000).toString(16).substr(0, 4) + '_' + (new Date()).getTime() + '_' + Math.random().toString().substr(2, 5)
}

window.document.addEventListener('debugger', function (e) {
  if (e.detail) {
    isDebugger = e.detail.isDebugger
  }
})

export default function (data) {
  if (isDebugger) {
    // 添加唯一值
    data.debugId = createRandomId()
    window.document.dispatchEvent(new CustomEvent('request', {
      detail: data
    }))
  }
  return new Promise((resolve) => {
    if (isDebugger) {
      window.document.addEventListener(data.debugId, () => {
        resolve(data)
      })
    } else {
      resolve()
    }
  })
}
